﻿Imports System.IO
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports System.Data.Common
Imports System.ComponentModel

'Authors 
'Golikidis Marios - Salabasis Mike

Public Class Form1

    'The constantPaddingForUserControlInterface variable is used 
    'to keep the constant distance between the 
    'UserControls(UserControlInterface),which are depicted on the 
    'main Form(Form1)
    'CONSTANT_PADDING_FOR_UserControlInterface = UserControlInterface.Height + 17
    Const CONSTANT_PADDING_FOR_UserControlInterface As Integer = 220

    'For sending to methods,which take data from database
    'These is changhed while programm is running
    Dim strSqlForPresenting As String = "SELECT adEventID FROM Adevents a  WHERE a.refEventID = -1 ORDER BY EventDate DESC "
    Dim strSqlCountForPresenting As String = "SELECT COUNT(*) AS C FROM Adevents a  WHERE a.refEventID = -1 ; "
    'Backup
    Dim firstStrSqlForPresenting As String = "SELECT adEventID FROM Adevents a  WHERE a.refEventID = -1 ORDER BY EventDate DESC "
    Dim firstStrSqlCountForPresenting As String = "SELECT COUNT(*) AS C FROM Adevents a  WHERE a.refEventID = -1 ; "

    'Just for help,Keeps Local the sql string for operations
    Dim strSQL

    'Keeps the input
    Dim search As String = ""

    'These variables keep the previous strings SQL query
    Dim previousSQLstr As String = ""
    Dim previousSQLstrCount As String = ""

    'Basic list of basic user controls
    Public Shared listForUserControlsInterface As New List(Of UserControlInterface)

    'The variable numberOfExistingUserControlsInterface is going 
    'to keep the total number of records(UserControlsInterface)  
    Public Shared numberOfExistingUserControlsInterface As Integer = 0


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'When the programm starts we call DisplayALL Sub and we present 
        'all the records of the basic table(Adevents) 
        'like UserControlInterface!!!!
        'Then the user will have the opportunity to do other operations
        DisplayALL(strSqlForPresenting, strSqlCountForPresenting)

        'For mouse wheel
        Me.PanelForAll.Select()

    End Sub

    Private Sub DisplayALL(ByVal strSQL, ByVal strSQLcount)
        Try

            listForUserControlsInterface.Clear()

            'First of all, in the strSqlCountForPresenting we want to get the total 
            'number of IDs(adEventID) from the right table(Adevents),
            'which associate with the expected result. 
            'As we said we are going to present appropriate records,
            'so we need to know how many records has the 
            'result. This number is the number of the list that we will
            'use.This list is going to keep the basic UserControl
            '(UserControlInterface). 
            'Also the variable strSqlCountForPresenting IS THE RESULT
            'with the INFO for any basic UserControl (UserControlInterface).

            '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            'numberOfExistingUserControlsInterface Like a Pointer
            DataBaseLibrary.getCountResultFromDataBase(strSQLcount, numberOfExistingUserControlsInterface)

            'So, as we have the total number of the records,we make the 
            'list for the basic UserControl(UserControlInterface),and of
            'course we ADD every UserControl in the main form to the 
            'appropriate location.
            'But here we have added the NUMBER of the basic 
            'UserControl(UserControlInterface),
            'that we are going to use.The user controls don't have any 
            'Information(yet!)
            For i As Integer = 0 To numberOfExistingUserControlsInterface - 1
                Dim elementOfUserControls = New UserControlInterface()

                elementOfUserControls.AddComment.Tag = i
                elementOfUserControls.DeleteButton.Tag = i
                elementOfUserControls.PanelForReferences.Tag = i

                elementOfUserControls.Location = New Point((My.Computer.Screen.Bounds.Width - elementOfUserControls.Width) / 2, i * CONSTANT_PADDING_FOR_UserControlInterface)

                Dim he = i * CONSTANT_PADDING_FOR_UserControlInterface

                Me.PanelForAll.Controls.Add(elementOfUserControls)
                listForUserControlsInterface.Add(elementOfUserControls)
            Next


            'At this point we want to add information at the basic 
            'UserControls.So we have to connect with the database and 
            'for every adEventID[refEventID = null] (=>UserControlInterface) we will take 
            'the appropriate information,which will be depicted 
            'at the basic UserControls.
            DataBaseLibrary.addInfoUserControlsFromDataBase(strSQL, listForUserControlsInterface)

        Catch ex As Exception
            MsgBox("Problem in Displaying.", vbExclamation, "BE CAREFUL")
        End Try

        'For mouse wheel
        Me.PanelForAll.Select()

    End Sub

    'This method is not like the others.It is used for 
    'initialization.in addition, it is used for basic operation,
    'which will call the dispay method,like searching,filtering etc. 
    Public Sub BeForDispaying()

        Try
            'SELECT about the appropriate number of basic UserControl
            Dim count As Integer
            DataBaseLibrary.getCountResultFromDataBase(strSqlCountForPresenting, count)

            If count > 0 Then

                'Here we create the appropriate panel
                Me.PanelForAll.Controls.Clear()

                'Here we call the DisplayALL method with 2 SELECT QUERIES
                DisplayALL(strSqlForPresenting, strSqlCountForPresenting)

            Else
                strSqlForPresenting = previousSQLstr
                strSqlCountForPresenting = previousSQLstrCount
                MsgBox("Wrong input!Please enter again your String.", vbExclamation, "ERROR")
            End If

        Catch ex As Exception
            MsgBox("Problem in Displaying initialization.", vbExclamation, "BE CAREFUL")
        End Try

    End Sub

    Private Sub Form1_Click(sender As Object, e As System.EventArgs) Handles Me.Click
        'For mouse wheel
        Me.PanelForAll.Select()
    End Sub

    Private Sub Form1_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        'For mouse wheel
        Me.PanelForAll.Select()
    End Sub

    Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        'For mouse wheel
        Me.PanelForAll.Select()
    End Sub

    Private Sub Form1_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDoubleClick
        'For mouse wheel
        Me.PanelForAll.Select()
    End Sub

    Private Sub Form1_MouseEnter(sender As Object, e As System.EventArgs) Handles Me.MouseEnter
        'For mouse wheel
        Me.PanelForAll.Select()
    End Sub

    Private Sub Form1_MouseLeave(sender As Object, e As System.EventArgs) Handles Me.MouseLeave
        'For mouse wheel
        Me.PanelForAll.Select()
    End Sub

    Private Sub Form1_MouseHover(sender As Object, e As System.EventArgs) Handles Me.MouseHover
        'For mouse wheel
        Me.PanelForAll.Select()
    End Sub

    Private Sub PanelForAll_Click(sender As Object, e As System.EventArgs) Handles Me.Click
        'For mouse wheel
        Me.PanelForAll.Select()
    End Sub

    Private Sub PanelForAll_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        'For mouse wheel
        Me.PanelForAll.Select()
    End Sub

    Private Sub PanelForAll_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        'For mouse wheel
        Me.PanelForAll.Select()
    End Sub

    Private Sub PanelForAll_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDoubleClick
        'For mouse wheel
        Me.PanelForAll.Select()
    End Sub

    Private Sub PanelForAll_MouseEnter(sender As Object, e As System.EventArgs) Handles Me.MouseEnter
        'For mouse wheel
        Me.PanelForAll.Select()
    End Sub

    Private Sub PanelForAll_MouseLeave(sender As Object, e As System.EventArgs) Handles Me.MouseLeave
        'For mouse wheel
        Me.PanelForAll.Select()
    End Sub

    Private Sub PanelForAll_MouseHover(sender As Object, e As System.EventArgs) Handles Me.MouseHover
        'For mouse wheel
        Me.PanelForAll.Select()
    End Sub

    '///////////////////////////////////////////////////////////////////////////////////// 






    '////////////////////// METHODS OF TOOL STRIP INTERFACE CONTROL //////////////////////

    Private Sub RefreshThis_Click(sender As System.Object, e As System.EventArgs) Handles RefreshThis.Click
        'If we want refresh,after searching or filtering
        Try
            Me.PanelForAll.Controls.Clear()
            DisplayALL(strSqlForPresenting, strSqlCountForPresenting)
        Catch ex As Exception
            MsgBox("Refresh Problem", vbExclamation, "BE CAREFUL")
        End Try

    End Sub

    Private Sub RefreshAll_Click(sender As System.Object, e As System.EventArgs) Handles RefreshAll.Click
        Try
            Me.PanelForAll.Controls.Clear()
            DisplayALL(firstStrSqlForPresenting, firstStrSqlCountForPresenting)
        Catch ex As Exception
            MsgBox("Refresh Problem", vbExclamation, "BE CAREFUL")
        End Try

    End Sub

    Private Sub ControlSave_Click(sender As System.Object, e As System.EventArgs) Handles ControlSave.Click
        'This Method is used from another class
        ControlSaving(True)
    End Sub

    Public Sub ControlSaving(ByVal mssage As Boolean)

        'Ok,here we are going to save the info of basic user control
        'either if the basic user control(UserControlInterface) is 
        'new or we have changed an older.


        Try
            'This is about autoSave or not 
            If (mssage = False) Then

                'Auto

                'We look the list of basic user control by DESC
                'and we save the user controls one by one
                For i = listForUserControlsInterface.Count - 1 To 0 Step -1

                    'We look now if ID of a specific user control exists in database(Adevents)  
                    If DataBaseLibrary.IdRecordExists(listForUserControlsInterface(i).getAdvertEventID, "Adevents", "adEventID") = False Then


                        'if ID not exists we take the data from the user control 

                        'Fisrt set the data in the list element
                        listForUserControlsInterface(i).setEventSiteValue(listForUserControlsInterface(i).getEventSiteValue())
                        listForUserControlsInterface(i).setEventAdValue(listForUserControlsInterface(i).getEventAdValue())
                        listForUserControlsInterface(i).setEventTypeValue(listForUserControlsInterface(i).getEventTypeValue())
                        listForUserControlsInterface(i).setEventUserValue(listForUserControlsInterface(i).getEventUserValue())
                        listForUserControlsInterface(i).setEventAdvertiserValue(listForUserControlsInterface(i).getEventAdvertiserValue())
                        listForUserControlsInterface(i).setRecordingValue(listForUserControlsInterface(i).getRecordingValue())

                        listForUserControlsInterface(i).setEventValue(listForUserControlsInterface(i).getEventValue())
                        listForUserControlsInterface(i).setEventDateValue(listForUserControlsInterface(i).getEventDateValue())

                        'Set a list for references
                        Dim listOfReferencesForSpecificUserControls As New List(Of UserControlReferences)
                        listForUserControlsInterface(i).setReferenceList(listOfReferencesForSpecificUserControls)


                        'For every value we look about the integrity of data indatabase
                        'and then insert thr data to database

                        Dim SiteID = DataBaseLibrary.RecordExists("SiteConnection", "eventSiteID", "SiteName", listForUserControlsInterface(i).getEventSiteValue())
                        If SiteID = 0 Then
                            SiteID = CInt(Math.Ceiling(Rnd() * 99).ToString & "2")
                            While DataBaseLibrary.IdRecordExists(SiteID.ToString, "SiteConnection", "eventSiteID") = True
                                SiteID = CInt(Math.Ceiling(Rnd() * 99).ToString & "2")
                            End While
                            listForUserControlsInterface(i).setSiteEventID(SiteID)
                            strSQL = " INSERT INTO SiteConnection(eventSiteID, SiteName) VALUES (" & SiteID & " , '" & listForUserControlsInterface(i).getEventSiteValue() & "');"
                            DataBaseLibrary.updateUserControlsFromDataBase(strSQL)
                        End If

                        Dim AdID = DataBaseLibrary.RecordExists("AdConnection", "eventAdID", "AdName", listForUserControlsInterface(i).getEventAdValue())
                        If AdID = 0 Then
                            AdID = CInt(Math.Ceiling(Rnd() * 99).ToString & "3")
                            While DataBaseLibrary.IdRecordExists(AdID.ToString, "AdConnection", "eventAdID") = True
                                AdID = CInt(Math.Ceiling(Rnd() * 99).ToString & "3")
                            End While
                            listForUserControlsInterface(i).setEventAdID(AdID)
                            strSQL = " INSERT INTO AdConnection(eventAdID, AdName) VALUES ( " & AdID & " , '" & listForUserControlsInterface(i).getEventAdValue() & "');"
                            DataBaseLibrary.updateUserControlsFromDataBase(strSQL)
                        End If

                        Dim TypeID = DataBaseLibrary.RecordExists("TypeConnection", "eventTypeID", "TypeName", listForUserControlsInterface(i).getEventTypeValue())
                        If TypeID = 0 Then
                            TypeID = CInt(Math.Ceiling(Rnd() * 99).ToString & "4")
                            While DataBaseLibrary.IdRecordExists(TypeID.ToString, "TypeConnection", "eventTypeID") = True
                                TypeID = CInt(Math.Ceiling(Rnd() * 99).ToString & "4")
                            End While
                            listForUserControlsInterface(i).setTypeEventID(TypeID)
                            strSQL = " INSERT INTO TypeConnection(eventTypeID, TypeName) VALUES ( " & TypeID & ", '" & listForUserControlsInterface(i).getEventTypeValue() & "');"
                            DataBaseLibrary.updateUserControlsFromDataBase(strSQL)
                        End If


                        Dim UserID = DataBaseLibrary.RecordExists("UserConnection", "eventUserID", "UserName", listForUserControlsInterface(i).getEventUserValue())
                        If UserID = 0 Then
                            UserID = CInt(Math.Ceiling(Rnd() * 99).ToString & "5")
                            While DataBaseLibrary.IdRecordExists(UserID.ToString, "UserConnection", "eventUserID") = True
                                UserID = CInt(Math.Ceiling(Rnd() * 99).ToString & "5")
                            End While
                            listForUserControlsInterface(i).setUserEventID(UserID)
                            strSQL = " INSERT INTO UserConnection(eventUserID, UserName) VALUES ( " & UserID & ", '" & listForUserControlsInterface(i).getEventUserValue() & "');"
                            DataBaseLibrary.updateUserControlsFromDataBase(strSQL)
                        End If


                        Dim AdvertiserID = DataBaseLibrary.RecordExists("AdvertiserConnection", "eventAdvertiserID", "AdvertiserName", listForUserControlsInterface(i).getEventAdvertiserValue())
                        If AdvertiserID = 0 Then
                            AdvertiserID = CInt(Math.Ceiling(Rnd() * 99).ToString & "6")
                            While DataBaseLibrary.IdRecordExists(AdvertiserID.ToString, "AdvertiserConnection", "eventAdvertiserID") = True
                                AdvertiserID = CInt(Math.Ceiling(Rnd() * 99).ToString & "6")
                            End While
                            listForUserControlsInterface(i).setAdvertiserEventID(AdvertiserID)
                            strSQL = " INSERT INTO AdvertiserConnection(eventAdvertiserID, AdvertiserName) VALUES ( " & AdvertiserID & ", '" & listForUserControlsInterface(i).getEventAdvertiserValue() & "');"
                            DataBaseLibrary.updateUserControlsFromDataBase(strSQL)
                        End If


                        Dim RecID = DataBaseLibrary.RecordExists("RecordingConnection", "RecordingID", "RecordingName", listForUserControlsInterface(i).getRecordingValue())
                        If RecID = 0 Then
                            RecID = CInt(Math.Ceiling(Rnd() * 99).ToString & "7")
                            While DataBaseLibrary.IdRecordExists(RecID.ToString, "RecordingConnection", "RecordingID") = True
                                RecID = CInt(Math.Ceiling(Rnd() * 99).ToString & "7")
                            End While
                            listForUserControlsInterface(i).setRecordingID(RecID)
                            strSQL = " INSERT INTO RecordingConnection(RecordingID, RecordingName) VALUES ( " & RecID & ", '" & listForUserControlsInterface(i).getRecordingValue() & "');"
                            DataBaseLibrary.updateUserControlsFromDataBase(strSQL)
                        End If

                        Dim ID = CInt(Math.Ceiling(Rnd() * 99).ToString & "1")
                        While DataBaseLibrary.IdRecordExists(ID.ToString, "Adevents", "adEventID") = True
                            ID = CInt(Math.Ceiling(Rnd() * 99).ToString & "1")
                        End While
                        listForUserControlsInterface(i).setAdvertEventID(ID)

                        strSQL = " INSERT INTO Adevents( " & _
                                 " adEventID, " & _
                                 " eventSiteID, " & _
                                 " eventAdID, " & _
                                 " eventTypeID, " & _
                                 " eventUserID, " & _
                                 " EventDate, " & _
                                 " eventAdvertiserID, " & _
                                 " RecordingID, " & _
                                 " eventValue, " & _
                                 " refEventID) VALUES " & _
                                 " ( " & ID & " , " & SiteID & " , " & AdID & " , " & TypeID & " , " & UserID & " , " & DataBaseLibrary.ConvertDateForThisDataBase(listForUserControlsInterface(i).getEventDateValue().ToString) & " , " & AdvertiserID & " , " & RecID & " , '" & listForUserControlsInterface(i).getEventValue() & "' ,  -1  ) ; "

                        DataBaseLibrary.updateUserControlsFromDataBase(strSQL)
                    Else


                        'if ID exists we take the data from the user control 
                        'update the data

                        strSQL = " UPDATE Adevents " & _
                                 " SET " & _
                                 " EventDate = " & _
                                 DataBaseLibrary.ConvertDateForThisDataBase(listForUserControlsInterface(i).getEventDateValue().ToString) & _
                                 " WHERE " & _
                                 " adEventID = " & listForUserControlsInterface(i).getAdvertEventID() & ";"

                        DataBaseLibrary.updateUserControlsFromDataBase(strSQL)

                        strSQL = " UPDATE Adevents " & _
                                 " SET " & _
                                 " eventValue = '" & _
                                 listForUserControlsInterface(i).getEventValue() & _
                                 "' WHERE " & _
                                 " adEventID = " & listForUserControlsInterface(i).getAdvertEventID() & ";"

                        DataBaseLibrary.updateUserControlsFromDataBase(strSQL)

                        strSQL = " UPDATE SiteConnection SET SiteName = '" & listForUserControlsInterface(i).getEventSiteValue() & "' WHERE eventSiteID = " & listForUserControlsInterface(i).getSiteEventID() & ";"
                        DataBaseLibrary.updateUserControlsFromDataBase(strSQL)

                        strSQL = " UPDATE AdConnection SET AdName = '" & listForUserControlsInterface(i).getEventAdValue() & "' WHERE eventAdID = " & listForUserControlsInterface(i).getEventAdID() & ";"
                        DataBaseLibrary.updateUserControlsFromDataBase(strSQL)

                        strSQL = " UPDATE TypeConnection SET TypeName = '" & listForUserControlsInterface(i).getEventTypeValue() & "' WHERE eventTypeID = " & listForUserControlsInterface(i).getTypeEventID() & ";"
                        DataBaseLibrary.updateUserControlsFromDataBase(strSQL)

                        strSQL = " UPDATE UserConnection SET UserName = '" & listForUserControlsInterface(i).getEventUserValue() & "' WHERE eventUserID = " & listForUserControlsInterface(i).getUserEventID() & ";"
                        DataBaseLibrary.updateUserControlsFromDataBase(strSQL)

                        strSQL = " UPDATE AdvertiserConnection SET AdvertiserName = '" & listForUserControlsInterface(i).getEventAdvertiserValue() & "' WHERE eventAdvertiserID = " & listForUserControlsInterface(i).getAdvertiserEventID() & ";"
                        DataBaseLibrary.updateUserControlsFromDataBase(strSQL)

                        strSQL = " UPDATE RecordingConnection SET RecordingName = '" & listForUserControlsInterface(i).getRecordingValue() & "' WHERE RecordingID = " & listForUserControlsInterface(i).getRecordingID() & ";"
                        DataBaseLibrary.updateUserControlsFromDataBase(strSQL)


                    End If
                Next




            Else



                Dim ret = MsgBox("Are you sure about SAVE?", vbYesNo + vbCritical, "BE CAREFUL")
                If (ret.ToString = "Yes") = True Then



                    'We look the list of basic user control by DESC
                    'and we save the user controls one by one
                    For i = listForUserControlsInterface.Count - 1 To 0 Step -1

                        'We look now if ID of a specific user control exists in database(Adevents)  
                        If DataBaseLibrary.IdRecordExists(listForUserControlsInterface(i).getAdvertEventID, "Adevents", "adEventID") = False Then

                            'if ID not exists we take the data from the user control 

                            'Fisrt set the data in the list element
                            listForUserControlsInterface(i).setEventSiteValue(listForUserControlsInterface(i).getEventSiteValue())
                            listForUserControlsInterface(i).setEventAdValue(listForUserControlsInterface(i).getEventAdValue())
                            listForUserControlsInterface(i).setEventTypeValue(listForUserControlsInterface(i).getEventTypeValue())
                            listForUserControlsInterface(i).setEventUserValue(listForUserControlsInterface(i).getEventUserValue())
                            listForUserControlsInterface(i).setEventAdvertiserValue(listForUserControlsInterface(i).getEventAdvertiserValue())
                            listForUserControlsInterface(i).setRecordingValue(listForUserControlsInterface(i).getRecordingValue())

                            listForUserControlsInterface(i).setEventValue(listForUserControlsInterface(i).getEventValue())
                            listForUserControlsInterface(i).setEventDateValue(listForUserControlsInterface(i).getEventDateValue())

                            'Set a list for references
                            Dim listOfReferencesForSpecificUserControls As New List(Of UserControlReferences)
                            listForUserControlsInterface(i).setReferenceList(listOfReferencesForSpecificUserControls)


                            'For every value we look about the integrity of data indatabase
                            'and then insert thr data to database

                            Dim SiteID = DataBaseLibrary.RecordExists("SiteConnection", "eventSiteID", "SiteName", listForUserControlsInterface(i).getEventSiteValue())
                            If SiteID = 0 Then
                                SiteID = CInt(Math.Ceiling(Rnd() * 99).ToString & "2")
                                While DataBaseLibrary.IdRecordExists(SiteID.ToString, "SiteConnection", "eventSiteID") = True
                                    SiteID = CInt(Math.Ceiling(Rnd() * 99).ToString & "2")
                                End While
                                listForUserControlsInterface(i).setSiteEventID(SiteID)
                                strSQL = " INSERT INTO SiteConnection(eventSiteID, SiteName) VALUES (" & SiteID & " , '" & listForUserControlsInterface(i).getEventSiteValue() & "');"
                                DataBaseLibrary.updateUserControlsFromDataBase(strSQL)
                            End If

                            Dim AdID = DataBaseLibrary.RecordExists("AdConnection", "eventAdID", "AdName", listForUserControlsInterface(i).getEventAdValue())
                            If AdID = 0 Then
                                AdID = CInt(Math.Ceiling(Rnd() * 99).ToString & "3")
                                While DataBaseLibrary.IdRecordExists(AdID.ToString, "AdConnection", "eventAdID") = True
                                    AdID = CInt(Math.Ceiling(Rnd() * 99).ToString & "3")
                                End While
                                listForUserControlsInterface(i).setEventAdID(AdID)
                                strSQL = " INSERT INTO AdConnection(eventAdID, AdName) VALUES ( " & AdID & " , '" & listForUserControlsInterface(i).getEventAdValue() & "');"
                                DataBaseLibrary.updateUserControlsFromDataBase(strSQL)
                            End If

                            Dim TypeID = DataBaseLibrary.RecordExists("TypeConnection", "eventTypeID", "TypeName", listForUserControlsInterface(i).getEventTypeValue())
                            If TypeID = 0 Then
                                TypeID = CInt(Math.Ceiling(Rnd() * 99).ToString & "4")
                                While DataBaseLibrary.IdRecordExists(TypeID.ToString, "TypeConnection", "eventTypeID") = True
                                    TypeID = CInt(Math.Ceiling(Rnd() * 99).ToString & "4")
                                End While
                                listForUserControlsInterface(i).setTypeEventID(TypeID)
                                strSQL = " INSERT INTO TypeConnection(eventTypeID, TypeName) VALUES ( " & TypeID & ", '" & listForUserControlsInterface(i).getEventTypeValue() & "');"
                                DataBaseLibrary.updateUserControlsFromDataBase(strSQL)
                            End If


                            Dim UserID = DataBaseLibrary.RecordExists("UserConnection", "eventUserID", "UserName", listForUserControlsInterface(i).getEventUserValue())
                            If UserID = 0 Then
                                UserID = CInt(Math.Ceiling(Rnd() * 99).ToString & "5")
                                While DataBaseLibrary.IdRecordExists(UserID.ToString, "UserConnection", "eventUserID") = True
                                    UserID = CInt(Math.Ceiling(Rnd() * 99).ToString & "5")
                                End While
                                listForUserControlsInterface(i).setUserEventID(UserID)
                                strSQL = " INSERT INTO UserConnection(eventUserID, UserName) VALUES ( " & UserID & ", '" & listForUserControlsInterface(i).getEventUserValue() & "');"
                                DataBaseLibrary.updateUserControlsFromDataBase(strSQL)
                            End If


                            Dim AdvertiserID = DataBaseLibrary.RecordExists("AdvertiserConnection", "eventAdvertiserID", "AdvertiserName", listForUserControlsInterface(i).getEventAdvertiserValue())
                            If AdvertiserID = 0 Then
                                AdvertiserID = CInt(Math.Ceiling(Rnd() * 99).ToString & "6")
                                While DataBaseLibrary.IdRecordExists(AdvertiserID.ToString, "AdvertiserConnection", "eventAdvertiserID") = True
                                    AdvertiserID = CInt(Math.Ceiling(Rnd() * 99).ToString & "6")
                                End While
                                listForUserControlsInterface(i).setAdvertiserEventID(AdvertiserID)
                                strSQL = " INSERT INTO AdvertiserConnection(eventAdvertiserID, AdvertiserName) VALUES ( " & AdvertiserID & ", '" & listForUserControlsInterface(i).getEventAdvertiserValue() & "');"
                                DataBaseLibrary.updateUserControlsFromDataBase(strSQL)
                            End If


                            Dim RecID = DataBaseLibrary.RecordExists("RecordingConnection", "RecordingID", "RecordingName", listForUserControlsInterface(i).getRecordingValue())
                            If RecID = 0 Then
                                RecID = CInt(Math.Ceiling(Rnd() * 99).ToString & "7")
                                While DataBaseLibrary.IdRecordExists(RecID.ToString, "RecordingConnection", "RecordingID") = True
                                    RecID = CInt(Math.Ceiling(Rnd() * 99).ToString & "7")
                                End While
                                listForUserControlsInterface(i).setRecordingID(RecID)
                                strSQL = " INSERT INTO RecordingConnection(RecordingID, RecordingName) VALUES ( " & RecID & ", '" & listForUserControlsInterface(i).getRecordingValue() & "');"
                                DataBaseLibrary.updateUserControlsFromDataBase(strSQL)
                            End If

                            Dim ID = CInt(Math.Ceiling(Rnd() * 99).ToString & "1")
                            While DataBaseLibrary.IdRecordExists(ID.ToString, "Adevents", "adEventID") = True
                                ID = CInt(Math.Ceiling(Rnd() * 99).ToString & "1")
                            End While
                            listForUserControlsInterface(i).setAdvertEventID(ID)

                            strSQL = " INSERT INTO Adevents( " & _
                                     " adEventID, " & _
                                     " eventSiteID, " & _
                                     " eventAdID, " & _
                                     " eventTypeID, " & _
                                     " eventUserID, " & _
                                     " EventDate, " & _
                                     " eventAdvertiserID, " & _
                                     " RecordingID, " & _
                                     " eventValue, " & _
                                     " refEventID) VALUES " & _
                                     " ( " & ID & " , " & SiteID & " , " & AdID & " , " & TypeID & " , " & UserID & " , " & DataBaseLibrary.ConvertDateForThisDataBase(listForUserControlsInterface(i).getEventDateValue().ToString) & " , " & AdvertiserID & " , " & RecID & " , '" & listForUserControlsInterface(i).getEventValue() & "' ,  -1  ) ; "

                            DataBaseLibrary.updateUserControlsFromDataBase(strSQL)




                        Else



                            'if ID exists we take the data from the user control 
                            'update the data

                            strSQL = " UPDATE Adevents " & _
                                     " SET " & _
                                     " EventDate = " & _
                                     DataBaseLibrary.ConvertDateForThisDataBase(listForUserControlsInterface(i).getEventDateValue().ToString) & _
                                     " WHERE " & _
                                     " adEventID = " & listForUserControlsInterface(i).getAdvertEventID() & ";"

                            DataBaseLibrary.updateUserControlsFromDataBase(strSQL)

                            strSQL = " UPDATE Adevents " & _
                                     " SET " & _
                                     " eventValue = '" & _
                                     listForUserControlsInterface(i).getEventValue() & _
                                     "' WHERE " & _
                                     " adEventID = " & listForUserControlsInterface(i).getAdvertEventID() & ";"

                            DataBaseLibrary.updateUserControlsFromDataBase(strSQL)

                            strSQL = " UPDATE SiteConnection SET SiteName = '" & listForUserControlsInterface(i).getEventSiteValue() & "' WHERE eventSiteID = " & listForUserControlsInterface(i).getSiteEventID() & ";"
                            DataBaseLibrary.updateUserControlsFromDataBase(strSQL)

                            strSQL = " UPDATE AdConnection SET AdName = '" & listForUserControlsInterface(i).getEventAdValue() & "' WHERE eventAdID = " & listForUserControlsInterface(i).getEventAdID() & ";"
                            DataBaseLibrary.updateUserControlsFromDataBase(strSQL)

                            strSQL = " UPDATE TypeConnection SET TypeName = '" & listForUserControlsInterface(i).getEventTypeValue() & "' WHERE eventTypeID = " & listForUserControlsInterface(i).getTypeEventID() & ";"
                            DataBaseLibrary.updateUserControlsFromDataBase(strSQL)

                            strSQL = " UPDATE UserConnection SET UserName = '" & listForUserControlsInterface(i).getEventUserValue() & "' WHERE eventUserID = " & listForUserControlsInterface(i).getUserEventID() & ";"
                            DataBaseLibrary.updateUserControlsFromDataBase(strSQL)

                            strSQL = " UPDATE AdvertiserConnection SET AdvertiserName = '" & listForUserControlsInterface(i).getEventAdvertiserValue() & "' WHERE eventAdvertiserID = " & listForUserControlsInterface(i).getAdvertiserEventID() & ";"
                            DataBaseLibrary.updateUserControlsFromDataBase(strSQL)

                            strSQL = " UPDATE RecordingConnection SET RecordingName = '" & listForUserControlsInterface(i).getRecordingValue() & "' WHERE RecordingID = " & listForUserControlsInterface(i).getRecordingID() & ";"
                            DataBaseLibrary.updateUserControlsFromDataBase(strSQL)


                        End If
                    Next



                End If
            End If

        Catch ex As Exception
            MsgBox("Problem in Saving.", vbExclamation, "BE CAREFUL")
        End Try

    End Sub

    Private Sub ControlAdd_Click(sender As System.Object, e As System.EventArgs) Handles ControlAdd.Click
        Try
            'The user wants to add a basic user control


            'This is used for the specific location in screen, in case when
            'the location is < 0 and the reason for this is the mouse wheel
            Dim PaddingforScreen = 0
            If (listForUserControlsInterface(0).Location.Y < 0) Then
                PaddingforScreen = listForUserControlsInterface(0).Location.Y
            End If


            'We change the location of all basic user controls
            'So, we can add the new basic user control


            For i = listForUserControlsInterface.Count - 1 To 0 Step -1
                listForUserControlsInterface(i).Location = New Point((My.Computer.Screen.Bounds.Width - listForUserControlsInterface(i).Width) / 2, listForUserControlsInterface(i).Location.Y + CONSTANT_PADDING_FOR_UserControlInterface)
                listForUserControlsInterface(i).AddComment.Tag = i + 1
                listForUserControlsInterface(i).DeleteButton.Tag = i + 1
                listForUserControlsInterface(i).PanelForReferences.Tag = i + 1
            Next

            'Set location of new basic control 
            Dim userControlInterfaceAdded = New UserControlInterface()
            userControlInterfaceAdded.Location = New Point((My.Computer.Screen.Bounds.Width - userControlInterfaceAdded.Width) / 2, PaddingforScreen)

            'Properties of new basic user control
            Dim timeStamp As DateTime = DateTime.Now
            userControlInterfaceAdded.setEventDateValue(timeStamp)
            userControlInterfaceAdded.AddComment.Tag = 0
            userControlInterfaceAdded.DeleteButton.Tag = 0
            userControlInterfaceAdded.PanelForReferences.Tag = 0

            'Add to Form
            Me.PanelForAll.Controls.Add(userControlInterfaceAdded)

            'Add to basic list
            listForUserControlsInterface.Insert(0, userControlInterfaceAdded)

        Catch ex As Exception
            MsgBox("Problem in Adding.", vbExclamation, "BE CAREFUL")
        End Try

    End Sub

    '///////////////////////////////////////////////////////////////////////////////////// 





    '///////////////////// METHODS OF USER CONTROL INTERFACE CONTROL /////////////////////

    '///////////////////////////////////// SEARCHING ///////////////////////////////////// 

    Private Sub SearchingBySiteName_Click(sender As System.Object, e As System.EventArgs) Handles SearchingBySiteName.Click
        'Input and Checks SQL Injections
        likeSanitization()
        If Not search = "" Then
            previousSQLstr = strSqlForPresenting
            previousSQLstrCount = strSqlCountForPresenting
            '1 SELECT about the Info we want
            '2 SELECT about the appropriate number of basic UserControl
            strSqlForPresenting = "SELECT adEventID " & _
                                    "FROM Adevents a,SiteConnection sc " & _
                                    "WHERE a.eventSiteID = sc.eventSiteID " & _
                                    "AND SiteName = '" & search & "' "
            strSqlCountForPresenting = "SELECT COUNT(*) AS C " & _
                                        "FROM Adevents a,SiteConnection sc " & _
                                        "WHERE a.eventSiteID = sc.eventSiteID " & _
                                        "AND SiteName = '" & search & "'; "

            'This begins the process of displaying the sulitable data 
            BeForDispaying()
        Else
            MsgBox("No name Entered.May be you Should try again.", vbExclamation, "BE CAREFUL")
        End If

    End Sub

    Private Sub SearchingByTypeName_Click(sender As System.Object, e As System.EventArgs) Handles SearchingByTypeName.Click

        'Input and Checks SQL Injections
        likeSanitization()
        If Not search = "" Then
            previousSQLstr = strSqlForPresenting
            previousSQLstrCount = strSqlCountForPresenting
            '1 SELECT about the Info we want
            '2 SELECT about the appropriate number of basic UserControl
            strSqlForPresenting = "SELECT adEventID " & _
                                    "FROM Adevents a,TypeConnection tc " & _
                                    "WHERE a.eventTypeID = tc.eventTypeID " & _
                                    "AND TypeName = '" & search & "' "
            strSqlCountForPresenting = "SELECT COUNT(*) AS C " & _
                                        "FROM Adevents a,TypeConnection tc " & _
                                        "WHERE a.eventTypeID = tc.eventTypeID " & _
                                        "AND TypeName = '" & search & "'; "

            'This begins the process of displaying the sulitable data 
            BeForDispaying()
        Else
            MsgBox("No name Entered.May be you Should try again.", vbExclamation, "BE CAREFUL")
        End If


    End Sub

    Private Sub SearchingByUserName_Click(sender As System.Object, e As System.EventArgs) Handles SearchingByUserName.Click

        'Input and Checks SQL Injections
        likeSanitization()
        If Not search = "" Then
            previousSQLstr = strSqlForPresenting
            previousSQLstrCount = strSqlCountForPresenting
            '1 SELECT about the Info we want
            '2 SELECT about the appropriate number of basic UserControl
            strSqlForPresenting = "SELECT adEventID " & _
                                    "FROM Adevents a,UserConnection uc " & _
                                    "WHERE a.eventUserID = uc.eventUserID " & _
                                    "AND UserName = '" & search & "' "
            strSqlCountForPresenting = "SELECT COUNT(*) AS C " & _
                                        "FROM Adevents a,UserConnection uc " & _
                                        "WHERE a.eventUserID = uc.eventUserID " & _
                                        "AND UserName = '" & search & "'; "

            'This begins the process of displaying the sulitable data 
            BeForDispaying()
        Else
            MsgBox("No name Entered.May be you Should try again.", vbExclamation, "BE CAREFUL")
        End If


    End Sub

    Private Sub SearchingByAdvertiserName_Click(sender As System.Object, e As System.EventArgs) Handles SearchingByAdvertiserName.Click

        'Input and Checks SQL Injections
        likeSanitization()
        If Not search = "" Then
            previousSQLstr = strSqlForPresenting
            previousSQLstrCount = strSqlCountForPresenting
            '1 SELECT about the Info we want
            '2 SELECT about the appropriate number of basic UserControl
            strSqlForPresenting = "SELECT adEventID " & _
                                    "FROM Adevents a,AdvertiserConnection ac " & _
                                    "WHERE a.eventAdvertiserID = ac.eventAdvertiserID " & _
                                    "AND AdvertiserName = '" & search & "' "
            strSqlCountForPresenting = "SELECT COUNT(*) AS C " & _
                                        "FROM Adevents a,AdvertiserConnection ac " & _
                                        "WHERE a.eventAdvertiserID = ac.eventAdvertiserID " & _
                                        "AND AdvertiserName = '" & search & "'; "

            'This begins the process of displaying the sulitable data 
            BeForDispaying()
        Else
            MsgBox("No name Entered.May be you Should try again.", vbExclamation, "BE CAREFUL")
        End If


    End Sub

    Private Sub SearchingByRecordingName_Click(sender As System.Object, e As System.EventArgs) Handles SearchingByRecordingName.Click

        'Input and Checks SQL Injections
        likeSanitization()
        If Not search = "" Then
            previousSQLstr = strSqlForPresenting
            previousSQLstrCount = strSqlCountForPresenting
            '1 SELECT about the Info we want
            '2 SELECT about the appropriate number of basic UserControl
            strSqlForPresenting = "SELECT adEventID " & _
                                    "FROM Adevents a,RecordingConnection rc " & _
                                    "WHERE a.RecordingID = rc.RecordingID " & _
                                    "AND RecordingName = '" & search & "' "
            strSqlCountForPresenting = "SELECT COUNT(*) AS C " & _
                                        "FROM Adevents a,RecordingConnection rc " & _
                                        "WHERE a.RecordingID = rc.RecordingID " & _
                                        "AND RecordingName = '" & search & "'; "

            'This begins the process of displaying the sulitable data 
            BeForDispaying()
        Else
            MsgBox("No name Entered.May be you Should try again.", vbExclamation, "BE CAREFUL")
        End If

    End Sub

    '///////////////////////////////////////////////////////////////////////////////////// 






    '///////////////////////////////////// FILTERING ///////////////////////////////////// 

    Private Sub FilteringBySiteName_Click(sender As System.Object, e As System.EventArgs) Handles FilteringBySiteName.Click

        'Input and Checks SQL Injections
        likeSanitization()
        If Not search = "" Then
            previousSQLstr = strSqlForPresenting
            previousSQLstrCount = strSqlCountForPresenting
            '1 SELECT about the Info we want
            '2 SELECT about the appropriate number of basic UserControl
            strSqlForPresenting = "SELECT adEventID " & _
                                    "FROM Adevents a,SiteConnection sc " & _
                                    "WHERE a.eventSiteID = sc.eventSiteID " & _
                                    "AND a.refEventID = -1 AND SiteName LIKE '%" & search & "%' "
            strSqlCountForPresenting = "SELECT COUNT(*) AS C " & _
                                    "FROM Adevents a,SiteConnection sc " & _
                                    "WHERE a.eventSiteID = sc.eventSiteID " & _
                                    "AND a.refEventID = -1 AND SiteName LIKE '%" & search & "%'; "

            'This begins the process of displaying the sulitable data 
            BeForDispaying()
        Else
            MsgBox("No name Entered.May be you Should try again.", vbExclamation, "BE CAREFUL")
        End If

    End Sub

    Private Sub FilteringByTypeName_Click(sender As System.Object, e As System.EventArgs) Handles FilteringByTypeName.Click

        'Input and Checks SQL Injections
        likeSanitization()
        If Not search = "" Then
            previousSQLstr = strSqlForPresenting
            previousSQLstrCount = strSqlCountForPresenting
            '1 SELECT about the Info we want
            '2 SELECT about the appropriate number of basic UserControl
            strSqlForPresenting = "SELECT adEventID " & _
                                    "FROM Adevents a,TypeConnection tc " & _
                                    "WHERE a.eventTypeID = tc.eventTypeID " & _
                                    "AND a.refEventID = -1 AND TypeName LIKE '%" & search & "%' "
            strSqlCountForPresenting = "SELECT COUNT(*) AS C " & _
                                        "FROM Adevents a,TypeConnection tc " & _
                                        "WHERE a.eventTypeID = tc.eventTypeID " & _
                                        "AND a.refEventID = -1 AND TypeName LIKE '%" & search & "%'; "

            'This begins the process of displaying the sulitable data 
            BeForDispaying()
        Else
            MsgBox("No name Entered.May be you Should try again.", vbExclamation, "BE CAREFUL")
        End If

    End Sub

    Private Sub FilteringByUserName_Click(sender As System.Object, e As System.EventArgs) Handles FilteringByUserName.Click

        'Input and Checks SQL Injections
        likeSanitization()
        If Not search = "" Then
            previousSQLstr = strSqlForPresenting
            previousSQLstrCount = strSqlCountForPresenting
            '1 SELECT about the Info we want
            '2 SELECT about the appropriate number of basic UserControl
            strSqlForPresenting = "SELECT adEventID " & _
                                    "FROM Adevents a,UserConnection uc " & _
                                    "WHERE a.eventUserID = uc.eventUserID " & _
                                    "AND a.refEventID = -1 AND UserName LIKE '%" & search & "%' "
            strSqlCountForPresenting = "SELECT COUNT(*) AS C " & _
                                        "FROM Adevents a,UserConnection uc " & _
                                        "WHERE a.eventUserID = uc.eventUserID " & _
                                        "AND a.refEventID = -1 AND UserName LIKE '%" & search & "%'; "

            'This begins the process of displaying the sulitable data 
            BeForDispaying()
        Else
            MsgBox("No name Entered.May be you Should try again.", vbExclamation, "BE CAREFUL")
        End If

    End Sub

    Private Sub FilteringByAdvertiserName_Click(sender As System.Object, e As System.EventArgs) Handles FilteringByAdvertiserName.Click

        'Input and Checks SQL Injections
        likeSanitization()
        If Not search = "" Then
            previousSQLstr = strSqlForPresenting
            previousSQLstrCount = strSqlCountForPresenting
            '1 SELECT about the Info we want
            '2 SELECT about the appropriate number of basic UserControl
            strSqlForPresenting = "SELECT adEventID " & _
                                    "FROM Adevents a,AdvertiserConnection ac " & _
                                    "WHERE a.eventAdvertiserID = ac.eventAdvertiserID " & _
                                    "AND a.refEventID = -1 AND AdvertiserName LIKE '%" & search & "%' "
            strSqlCountForPresenting = "SELECT COUNT(*) AS C " & _
                                        "FROM Adevents a,AdvertiserConnection ac " & _
                                        "WHERE a.eventAdvertiserID = ac.eventAdvertiserID " & _
                                        "AND a.refEventID = -1 AND AdvertiserName LIKE '%" & search & "%'; "

            'This begins the process of displaying the sulitable data 
            BeForDispaying()
        Else
            MsgBox("No name Entered.May be you Should try again.", vbExclamation, "BE CAREFUL")
        End If

    End Sub

    Private Sub FilteringByRecordingName_Click(sender As System.Object, e As System.EventArgs) Handles FilteringByRecordingName.Click

        'Input and Checks SQL Injections
        likeSanitization()
        If Not search = "" Then
            previousSQLstr = strSqlForPresenting
            previousSQLstrCount = strSqlCountForPresenting
            '1 SELECT about the Info we want
            '2 SELECT about the appropriate number of basic UserControl
            strSqlForPresenting = "SELECT adEventID " & _
                                    "FROM Adevents a,RecordingConnection rc " & _
                                    "WHERE a.RecordingID = rc.RecordingID " & _
                                    "AND a.refEventID = -1 AND RecordingName LIKE '%" & search & "%' "
            strSqlCountForPresenting = "SELECT COUNT(*) AS C " & _
                                        "FROM Adevents a,RecordingConnection rc " & _
                                        "WHERE a.RecordingID = rc.RecordingID " & _
                                        "AND a.refEventID = -1 AND RecordingName LIKE '%" & search & "%'; "

            'This begins the process of displaying the sulitable data 
            BeForDispaying()
        Else
            MsgBox("No name Entered.May be you Should try again.", vbExclamation, "BE CAREFUL")
        End If

    End Sub

    '///////////////////////////////////////////////////////////////////////////////////// 






    '////////////////////////////////////// SORTING ////////////////////////////////////// 

    Private Sub SortingAllBySiteName_Click(sender As System.Object, e As System.EventArgs) Handles SortingAllBySiteName.Click
        Try
            'Here we create the appropriate panel
            Me.PanelForAll.Controls.Clear()

            'Here we call the DisplayALL method with 2 SELECT QUERIES
            '1 SELECT about the Info we want
            '2 SELECT about the appropriate number of basic UserControl
            strSqlForPresenting = " SELECT adEventID " & _
                                    "FROM Adevents a,SiteConnection sc " & _
                                    "WHERE a.eventSiteID = sc.eventSiteID AND a.refEventID = -1 " & _
                                    "ORDER BY SiteName "
            strSqlCountForPresenting = " SELECT COUNT(*) AS C " & _
                                        "FROM Adevents a,SiteConnection sc " & _
                                        "WHERE a.eventSiteID = sc.eventSiteID AND a.refEventID = -1 ; "

            DisplayALL(strSqlForPresenting, strSqlCountForPresenting)
        Catch ex As Exception
            MsgBox("Problem in Sorting.", vbExclamation, "BE CAREFUL")
        End Try
    End Sub

    Private Sub SortingAllByTypeName_Click(sender As System.Object, e As System.EventArgs) Handles SortingAllByTypeName.Click
        Try
            'Here we create the appropriate panel
            Me.PanelForAll.Controls.Clear()

            'Here we call the DisplayALL method with 2 SELECT QUERIES
            '1 SELECT about the Info we want
            '2 SELECT about the appropriate number of basic UserControl
            strSqlForPresenting = " SELECT adEventID " & _
                                    "FROM Adevents a,TypeConnection tc " & _
                                    "WHERE a.eventTypeID = tc.eventTypeID  AND a.refEventID = -1 " & _
                                    "ORDER BY TypeName "
            strSqlCountForPresenting = " SELECT COUNT(*) AS C " & _
                                        "FROM Adevents a,TypeConnection tc " & _
                                        "WHERE a.eventTypeID = tc.eventTypeID AND a.refEventID = -1 ; "
            DisplayALL(strSqlForPresenting, strSqlCountForPresenting)
        Catch ex As Exception
            MsgBox("Problem in Sorting.", vbExclamation, "BE CAREFUL")
        End Try
    End Sub

    Private Sub SortingAllByUserName_Click(sender As System.Object, e As System.EventArgs) Handles SortingAllByUserName.Click
        Try
            'Here we create the appropriate panel
            Me.PanelForAll.Controls.Clear()

            'Here we call the DisplayALL method with 2 SELECT QUERIES
            '1 SELECT about the Info we want
            '2 SELECT about the appropriate number of basic UserControl
            strSqlForPresenting = " SELECT adEventID " & _
                                    "FROM Adevents a,UserConnection uc " & _
                                    "WHERE a.eventUserID = uc.eventUserID AND a.refEventID = -1 " & _
                                    "ORDER BY UserName "
            strSqlCountForPresenting = " SELECT COUNT(*) AS C " & _
                                        "FROM Adevents a,UserConnection uc " & _
                                        "WHERE a.eventUserID = uc.eventUserID AND a.refEventID = -1 ; "
            DisplayALL(strSqlForPresenting, strSqlCountForPresenting)
        Catch ex As Exception
            MsgBox("Problem in Sorting.", vbExclamation, "BE CAREFUL")
        End Try
    End Sub

    Private Sub SortingAllByAdvertiserName_Click(sender As System.Object, e As System.EventArgs) Handles SortingAllByAdvertiserName.Click
        Try
            'Here we create the appropriate panel
            Me.PanelForAll.Controls.Clear()

            'Here we call the DisplayALL method with 2 SELECT QUERIES
            '1 SELECT about the Info we want
            '2 SELECT about the appropriate number of basic UserControl
            strSqlForPresenting = " SELECT adEventID " & _
                                    "FROM Adevents a,AdvertiserConnection ac " & _
                                    "WHERE a.eventAdvertiserID = ac.eventAdvertiserID AND a.refEventID = -1 " & _
                                    "ORDER BY AdvertiserName "
            strSqlCountForPresenting = " SELECT COUNT(*) AS C " & _
                                        "FROM Adevents a,AdvertiserConnection ac " & _
                                        "WHERE a.eventAdvertiserID = ac.eventAdvertiserID AND a.refEventID = -1 ; "
            DisplayALL(strSqlForPresenting, strSqlCountForPresenting)
        Catch ex As Exception
            MsgBox("Problem in Sorting.", vbExclamation, "BE CAREFUL")
        End Try
    End Sub

    Private Sub SortingAllByRecordingName_Click(sender As System.Object, e As System.EventArgs) Handles SortingAllByRecordingName.Click
        Try
            'Here we create the appropriate panel
            Me.PanelForAll.Controls.Clear()

            'Here we call the DisplayALL method with 2 SELECT QUERIES
            '1 SELECT about the Info we want
            '2 SELECT about the appropriate number of basic UserControl
            strSqlForPresenting = " SELECT adEventID " & _
                                    "FROM Adevents a,RecordingConnection rc " & _
                                    "WHERE a.RecordingID = rc.RecordingID AND a.refEventID = -1 " & _
                                    "ORDER BY RecordingName "
            strSqlCountForPresenting = " SELECT COUNT(*) AS C " & _
                                        "FROM Adevents a,RecordingConnection rc " & _
                                        "WHERE a.RecordingID = rc.RecordingID AND a.refEventID = -1 ; "
            DisplayALL(strSqlForPresenting, strSqlCountForPresenting)
        Catch ex As Exception
            MsgBox("Problem in Sorting.", vbExclamation, "BE CAREFUL")
        End Try
    End Sub

    '///////////////////////////////////////////////////////////////////////////////////// 






    '//////////////////////////////////// SORTING ALL //////////////////////////////////// 

    Private Sub SortingBySiteName_Click(sender As System.Object, e As System.EventArgs) Handles SortingBySiteName.Click
        Try
            strSqlForPresenting = "SELECT adEventID " & _
                        "FROM Adevents a,SiteConnection sc " & _
                        "WHERE a.eventSiteID = sc.eventSiteID " & _
                        "AND adEventID IN ( " & strSqlForPresenting & " ) ORDER BY SiteName "

            'This begins the process of displaying the sulitable data 
            BeForDispaying()
        Catch ex As Exception
            MsgBox("Problem in Sorting.", vbExclamation, "BE CAREFUL")
        End Try
    End Sub

    Private Sub SortingByTypeName_Click(sender As System.Object, e As System.EventArgs) Handles SortingByTypeName.Click
        Try
            strSqlForPresenting = "SELECT adEventID " & _
                        "FROM Adevents a,TypeConnection tc " & _
                        "WHERE a.eventTypeID = tc.eventTypeID " & _
                        "AND adEventID IN ( " & strSqlForPresenting & " ) ORDER BY TypeName "

            'This begins the process of displaying the sulitable data 
            BeForDispaying()
        Catch ex As Exception
            MsgBox("Problem in Sorting.", vbExclamation, "BE CAREFUL")
        End Try
    End Sub

    Private Sub SortingByUserName_Click(sender As System.Object, e As System.EventArgs) Handles SortingByUserName.Click
        Try
            strSqlForPresenting = "SELECT adEventID " & _
                        "FROM Adevents a,UserConnection uc " & _
                        "WHERE a.eventUserID = uc.eventUserID " & _
                        "AND adEventID IN ( " & strSqlForPresenting & " ) ORDER BY UserName "

            'This begins the process of displaying the sulitable data 
            BeForDispaying()
        Catch ex As Exception
            MsgBox("Problem in Sorting.", vbExclamation, "BE CAREFUL")
        End Try
    End Sub

    Private Sub SortingByAdvertiserName_Click(sender As System.Object, e As System.EventArgs) Handles SortingByAdvertiserName.Click
        Try
            strSqlForPresenting = "SELECT adEventID " & _
                                    "FROM Adevents a,AdvertiserConnection ac " & _
                                    "WHERE a.eventAdvertiserID = ac.eventAdvertiserID " & _
                                    "AND adEventID IN ( " & strSqlForPresenting & " ) ORDER BY AdvertiserName "

            'This begins the process of displaying the sulitable data 
            BeForDispaying()
        Catch ex As Exception
            MsgBox("Problem in Sorting.", vbExclamation, "BE CAREFUL")
        End Try
    End Sub

    Private Sub SortingByRecordingName_Click(sender As System.Object, e As System.EventArgs) Handles SortingByRecordingName.Click
        Try
            strSqlForPresenting = "SELECT adEventID " & _
                        "FROM Adevents a,RecordingConnection rc " & _
                        "WHERE a.RecordingID = rc.RecordingID " & _
                        "AND adEventID IN ( " & strSqlForPresenting & " ) ORDER BY RecordingName "

            'This begins the process of displaying the sulitable data 
            BeForDispaying()
        Catch ex As Exception
            MsgBox("Problem in Sorting.", vbExclamation, "BE CAREFUL")
        End Try
    End Sub

    '////////////////////////////////////// SECURITY ////////////////////////////////////// 

    'Input and Checks SQL Injections
    Private Sub likeSanitization()
        Try
            search = InputBox("Enter your String ")
            While search.Contains("'")
                search = InputBox("Be Careful. Don't use the ' Character! ")
            End While
        Catch ex As Exception
            MsgBox("Problem in SECURITY.", vbExclamation, "BE CAREFUL")
        End Try
    End Sub

End Class
